﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace RantAIOCR.Common
{
    public class AppConfigurtaionServices
    {
        public static IConfiguration Configuration { get; set; }
        static AppConfigurtaionServices()
        {
            //ReloadOnChange = true 当appsettings.json被修改时重新加载
            Configuration = new ConfigurationBuilder()
                .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
                .Build();

        }

        public static void SaveChanges()
        {
            var configPath = Path.Combine(Directory.GetCurrentDirectory(), "appsettings.json");
            var jsonConfig = File.ReadAllText(configPath);

            var jObject = JsonConvert.DeserializeObject<JObject>(jsonConfig);
            if (jObject == null)
                return;
            foreach (var item in Configuration.AsEnumerable())
            {
                if (jObject.TryGetValue(item.Key, out _))
                {
                    jObject[item.Key] = item.Value;
                }
            }
            File.WriteAllText(configPath, JsonConvert.SerializeObject(jObject));
        }

        static public JObject Getjson()
        {
            string filepath = System.AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json";
            using (System.IO.StreamReader file = System.IO.File.OpenText(filepath))
            {
                using (JsonTextReader reader = new JsonTextReader(file))
                {
                    JObject jObject = (JObject)JToken.ReadFrom(reader);
                    return jObject;
                }
            }
        }
        ///写入json数据到json文件
        static public void Writejson(string filepath, JObject jObject)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(filepath))
            {
                file.Write(jObject.ToString());
            }
        }
        ///修改json文件中数据的某个值
        static public void updateitem(string key, string value)
        {
            string datapath = System.AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json";

            JObject jObject = Getjson();

            dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(jObject.ToString());
            dynamic newjsonObj = jsonObj;
            newjsonObj[key] = value;
            Writejson(datapath, newjsonObj);

        }
        static public void updateObject(string key, Peramter[] value)
        {
            string datapath = System.AppDomain.CurrentDomain.BaseDirectory + "\\appsettings.json";

            JObject jObject = Getjson();

            dynamic jsonObj = Newtonsoft.Json.JsonConvert.DeserializeObject(jObject.ToString());
            JObject newjsonObj = jsonObj;
            JObject jsonObject = new JObject
            {
                // 将数组序列化为 JArray，并作为属性添加到 JObject
                [key] = JArray.FromObject(value)
            };
            newjsonObj[key] = jsonObject.GetValue(key);

            // newjsonObj[key] = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonObject.ToString());
            Writejson(datapath, newjsonObj);

        }
        static public void WriteObject(Peramter peramter)
        { 
        
        }
    }

    public class Peramter
    {
        public int Threshold { get; set; }
        public int Width { get; set; }
        public int Delta { get; set; }
    }
}
